eletrotupi / tcc/ frontend/app/triggers/edit/[id].tsx22ce11e7f70ff2faf4796eef6498cd1ec65bbd31
5.0 KB | 179 lines Raw
1
import {
2
  View,
3
  StyleSheet,
4
  KeyboardAvoidingView,
5
  ScrollView,
6
  ActivityIndicator,
7
} from "react-native";
8
import { Text } from "@/components/ui/Text";
9
import { useState, useEffect } from "react";
10
import { router, useLocalSearchParams, Stack } from "expo-router";
11

12
import { Card } from "@/components/ui/Cards";
13
import { TextArea } from "@/components/ui/Input";
14
import { Button } from "@/components/ui/Button";
15
import { Section, SectionHeader } from "@/components/ui/Sections";
16
import { Spacing, Typography, Colors, BorderRadius } from "@/constants/theme";
17
import { Ionicons, MaterialIcons } from "@expo/vector-icons";
18
import { useUpdateTrigger, useTrigger } from "@/hooks";
19
import { CategoryChips, CategoryOption } from "@/components/ui/CategoryChips";
20
import { TriggerCategory } from "@/constants/triggers";
21
import { TRIGGER_CATEGORY_DEFINITIONS } from "@/constants/trigger-categories";
22
import { TimePicker } from "@/components/ui/TimePicker";
23

24
const TRIGGER_CATEGORIES: CategoryOption<TriggerCategory>[] =
25
  TRIGGER_CATEGORY_DEFINITIONS.map((d) => ({
26
    label: d.label,
27
    value: d.id,
28
    icon: <MaterialIcons size={16} color={d.color} name={d.icon} />,
29
  }));
30

31
export default function EditTrigger() {
32
  const { id } = useLocalSearchParams<{ id: string }>();
33
  const { data: trigger, isLoading } = useTrigger(id);
34
  const updateTrigger = useUpdateTrigger();
35

36
  const [moment, setMoment] = useState(new Date());
37
  const [comment, setComment] = useState("");
38
  const [category, setCategory] = useState("WORK");
39

40
  useEffect(() => {
41
    if (trigger) {
42
      setMoment(new Date(trigger.moment));
43
      setComment(trigger.comment || "");
44
      setCategory(trigger.category);
45
    }
46
  }, [trigger]);
47

48
  const save = async () => {
49
    const data = {
50
      moment,
51
      comment,
52
      category,
53
    };
54

55
    await updateTrigger.mutateAsync({ id: String(id), payload: data });
56
    router.back();
57
  };
58

59
  if (isLoading) {
60
    return (
61
      <View style={styles.centered}>
62
        <ActivityIndicator size="large" color={Colors.light.tint} />
63
      </View>
64
    );
65
  }
66

67
  return (
68
    <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
69
      <Stack.Screen options={{ title: "Editar Registro" }} />
70
      <ScrollView scrollEventThrottle={16}>
71
        <View style={styles.container}>
72
          <View style={styles.header}>
73
            <View style={styles.innerHeaderWrapper}>
74
              <View style={[styles.headerIcon, { backgroundColor: "#FFF7ED" }]}>
75
                <Ionicons name="flash-outline" size={20} color="#EA580C" />
76
              </View>
77

78
              <View style={styles.headerTitleSubtitle}>
79
                <Text style={styles.headerTitle}>Editar Gatilho</Text>
80
                <Text style={styles.headerSubtitle}>
81
                  Atualize os dados do acontecimento
82
                </Text>
83
              </View>
84
            </View>
85

86
            <Text style={styles.description}>
87
              Você pode editar este registro nos primeiros 5 minutos após a
88
              criação.
89
            </Text>
90
          </View>
91

92
          <Section>
93
            <SectionHeader title="Horário do Evento" />
94
            <TimePicker value={moment} onChange={setMoment} />
95
          </Section>
96

97
          <Section>
98
            <SectionHeader title="Selecionar uma categoria" />
99
            <CategoryChips
100
              options={TRIGGER_CATEGORIES}
101
              active={category}
102
              onChange={setCategory}
103
            />
104
          </Section>
105

106
          <Section>
107
            <SectionHeader title="Adicione detalhes" />
108

109
            <Card style={styles.mainCard}>
110
              <TextArea
111
                label="Comentários sobre"
112
                variant="darkGhost"
113
                onChangeText={(val) => setComment(val)}
114
                value={comment}
115
                minRows={5}
116
                maxRows={10}
117
                placeholder="Descreva o que gerou esse sentimento"
118
              />
119
            </Card>
120
          </Section>
121

122
          <Button
123
            title="Salvar Alterações"
124
            onPress={save}
125
            loading={updateTrigger.isPending}
126
            disabled={updateTrigger.isPending}
127
          />
128
        </View>
129
      </ScrollView>
130
    </KeyboardAvoidingView>
131
  );
132
}
133

134
const styles = StyleSheet.create({
135
  container: {
136
    gap: 24,
137
    paddingHorizontal: Spacing.containerPadding,
138
    paddingVertical: Spacing.sectionGap,
139
    marginBottom: 50,
140
  },
141
  mainCard: {
142
    padding: Spacing.cardGap,
143
  },
144
  header: {},
145
  headerIcon: {
146
    paddingVertical: 15,
147
    paddingHorizontal: 14,
148
    width: 48,
149
    borderRadius: BorderRadius.md,
150
  },
151
  innerHeaderWrapper: {
152
    flex: 1,
153
    flexDirection: "row",
154
    alignItems: "center",
155
  },
156
  headerTitleSubtitle: {
157
    marginLeft: 10,
158
  },
159
  headerTitle: {
160
    ...Typography.headlineMd,
161
  },
162
  headerSubtitle: {
163
    ...Typography.bodyMd,
164
    color: Colors.light.textSecondary,
165
  },
166
  description: {
167
    ...Typography.bodyMd,
168
    color: Colors.light.textSecondary,
169
    marginTop: 20,
170
  },
171
  keyboardView: {
172
    flex: 1,
173
  },
174
  centered: {
175
    flex: 1,
176
    justifyContent: "center",
177
    alignItems: "center",
178
  },
179
});